package laundry.ws;

import java.sql.Connection;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;

import com.laundry.lib.Database;
import com.laundry.lib.Machine;
import com.laundry.lib.MachineDateAvailability;
import com.laundry.tables.Booking;
import com.laundry.tables.LaundryMachine;

/**
 * @author thomas_manalil
 *
 */
@Path("/admin")
public class AdminWS {
	
	@GET
	@Path("/add")	
	@Produces("application/json")
	public  Machine  addMachine(@QueryParam("isLive")boolean isLive, @QueryParam("remarks") String remarks){	
		
		Machine machine = new Machine(isLive,remarks);
		LaundryMachine laundryMachine = new LaundryMachine();
		return laundryMachine.addMachine(machine);			
	}
	
	@GET
	@Path("/getmachines")
	@Produces("application/json")
	public ArrayList<Machine>  getAllMachines(@QueryParam("isLive") boolean isLive){
		LaundryMachine laundryMachine = new LaundryMachine();
		return laundryMachine.getMachines(isLive);
	}
	
	@GET
	@Path("/getmachinesondate")
	@Produces("application/json")
	public ArrayList<MachineDateAvailability>  getAllMachinesOnDate(@QueryParam("isLive") boolean isLive,@QueryParam("date") Date date){
		LaundryMachine laundryMachine = new LaundryMachine();
		return laundryMachine.getMachinesOnADate(date,isLive);
	}
	
	@POST	 
	@Path("/setmachinedateavailability")
	@Consumes("application/json")
	@Produces("application/json")
	public int setMachineAvailabilityOnaDate( ArrayList<MachineDateAvailability> machines){
		LaundryMachine laundryMachine = new LaundryMachine();
		int numberOfMachinesAdded=0;
		for(MachineDateAvailability md : machines){
			numberOfMachinesAdded+=laundryMachine.addMachineAvailability(md);
		}
		return numberOfMachinesAdded; 
	}
	
	@GET	 
	@Path("/openbooking")	
	@Produces("application/json")
	public String openBooking(@QueryParam("openBookingDate") Date date){
		String message="";
		LaundryMachine laundryMachine=new LaundryMachine();
		int numberOfAvailableMachines=laundryMachine.getMachineAvailability(date);
		if(numberOfAvailableMachines>0){
			message+= "Number Of Available Machines : "+numberOfAvailableMachines;
			Booking booking = Booking.instantiate();
			int numberOfSlotsOpened=booking.openBooking(date);
			message+=". >> Number of new slots opened : "+numberOfSlotsOpened;
		}
		else{
			message+="No machines available on the selected date, Please Set Machine Availability first.";
		}
		return message;
	}	
	
	 
}



